﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class Car_AccidentDetailDAO
    {
        /// <summary>
        /// 事故伤亡人员
        /// </summary>
        /// <param name="list">人员列表</param>
        /// <param name="bid">事故ID</param>
        /// <returns></returns>
        public int SaveAccidentDetail(IList<Car_AccidentDetail> list, int bid)
        {
            List<HelperModel> sqls = new List<HelperModel>();
            foreach (var item in list)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into Car_AccidentDetail(");
                strSql.Append("Name,Address,IDCard,AccidentId)");
                strSql.Append(" values (");
                strSql.Append("@Name,@Address,@IDCard,@AccidentId)");
                HelperModel ent = new HelperModel();
                SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,10),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@IDCard", SqlDbType.NVarChar,20),
                    new SqlParameter("@AccidentId", SqlDbType.Int)};
                parameters[0].Value = item.Name;
                parameters[1].Value = item.Address;
                parameters[2].Value = item.IDCard;
                parameters[3].Value = bid;
                ent.Sql = strSql.ToString();
                ent.Parameter = parameters;
                sqls.Add(ent);
            }
            return DbHelperSQL.ExecuteSqlTran(sqls);
        }
        /// <summary>
        /// 获得前几行数据
        /// <param name="bid">事故ID</param>
        /// </summary>
        public IList<Car_AccidentDetail> GetList(int bid)
        {
            IList<Car_AccidentDetail> list = new List<Car_AccidentDetail>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            strSql.Append(" Id,Name,Address,IDCard ");
            strSql.Append(" FROM Car_AccidentDetail ");
            strSql.Append(" where AccidentId=@AccidentId");
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@AccidentId", SqlDbType.Int),
            };
            parameters[0].Value = bid;
            var dt = DbHelperSQL.Query(CommandType.Text, strSql.ToString(), parameters);
            foreach (DataRow item in dt.Rows)
            {
                list.Add(DataRowToModel(item));
            }
            return list;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Car_AccidentDetail DataRowToModel(DataRow row)
        {
            Car_AccidentDetail model = new Car_AccidentDetail();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["Address"] != null)
                {
                    model.Address = row["Address"].ToString();
                }
                if (row["IDCard"] != null)
                {
                    model.IDCard = row["IDCard"].ToString();
                }
            }
            return model;
        }
    }
}
